package com.ycode.bang.anbang.network;

import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Response;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.JsonRequest;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;

import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.Map;

/**
 * Json请求
 * sunwei Created on 2015/8/10.
 * email: lx_sunwei@163.com.
 */
    public class GsonRequest<T> extends JsonRequest<T> {

        private final Gson mGson;
        private final Class<T> mClassType;
        private final Map<String, String> mHeaders;
        private final Response.Listener<T> mListener;
    private String json;

    public GsonRequest(int method, String url, Class<T> classType, JSONObject jsonRequest,
                           Response.Listener<T> listener, Response.ErrorListener errorListener) {
            this(method, url, classType, null, jsonRequest, listener, errorListener);
        }

        public GsonRequest(int method, String url, Class<T> classType, Map<String, String> headers,
                           JSONObject jsonRequest, Response.Listener<T> listener,
                           Response.ErrorListener errorListener) {
            super(method, url, (jsonRequest == null) ? null : jsonRequest.toString(), listener,
                    errorListener);
            mGson = new Gson();
            mClassType = classType;
            mHeaders = headers;
            mListener = listener;
        }

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            return mHeaders != null ? mHeaders : super.getHeaders();
        }

    public String getJson() {
        return json;
    }

    @Override
        protected Response<T> parseNetworkResponse(NetworkResponse networkResponse) {
            try {
                json = new String(networkResponse.data, HttpHeaderParser.parseCharset
                        (networkResponse.headers));
                return Response.success(mGson.fromJson(json, mClassType),
                        HttpHeaderParser.parseCacheHeaders(networkResponse));
            } catch (UnsupportedEncodingException e) {
                return Response.error(new ParseError(e));
            } catch (JsonSyntaxException e) {
                return Response.error(new ParseError(e));
            }
        }

        @Override
        protected void deliverResponse(T response) {
            mListener.onResponse(response);
        }
    }